package com.bank;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.config.*;
import sun.rmi.transport.Connection;

import javax.sql.DataSource;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Admin implements ActionListener{
    JMenuItem open1 = new JMenuItem("历史记录");
    JMenuItem queryById = new JMenuItem("按ID查询");
    JMenuItem queryByName = new JMenuItem("按名字查");
    JMenuItem addAccount=new JMenuItem("添加账户");
    JMenuItem menuItem = new JMenuItem("修改账户信息");
    JMenuItem menuItem1 = new JMenuItem("删除 账户");
    JButton jButton1=new JButton("查询");
    JTextField jTextField1=new JTextField(20);
    JButton jButton2=new JButton("查询");
    JTextField jTextField2=new JTextField(20);
    JTextField jTextField11=new JTextField(20);
    JTextField jTextField12=new JTextField(20);
    String[] item={"name","id"};
    String[] items={"name","id","paasword","balance","others"};
    JButton jButton3=new JButton("查询");
    JTextField jTextField3=new JTextField(20);
    AnnotationConfigApplicationContext actx=new AnnotationConfigApplicationContext(Config.class);
    DataSource ds=(DataSource) actx.getBean("datasource");
    sun.rmi.transport.Connection conn=(Connection) ds.getConnection();
//    ApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig.class);
//    DataSource d = (DataSource) ac.getBean("dataSource");
//    Connection c = d.getConnection(); //获取连接
    //Connection connection1 = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/db01","root","hsp");
    String s; JTable table;
    ResultSet rs1;JComboBox jComboBox;
    Object[][] rowData;
    String[] x=new String[20];
    String[] v=new String[7];
    JButton jButton11=new JButton("确认");
    JButton jButton12=new JButton("确认");
    JLabel jLabel101=new JLabel("更新目标");
    JLabel jLabel102=new JLabel("更改字段");
    JLabel jLabel103=new JLabel("         ");
    JLabel jLabel104=new JLabel("         ");
    JComboBox j101,j102;
    JTextField jTextField101=new JTextField(20);
    JTextField jTextField102=new JTextField(20);
    public Admin(ResultSet rs) throws SQLException {
        rs1=rs;
        rs.absolute(0);
        jButton11.addActionListener(this);
        jButton12.addActionListener(this);
        jButton1.addActionListener(this);
        JFrame jf = new JFrame("管理员界 " );
        JMenuBar menuBar = new JMenuBar();
        jf.setJMenuBar(menuBar);
        JMenu mFile = new JMenu("账户");
        JMenu mEdit = new JMenu("功能");
        menuBar.add(mFile);
        menuBar.add(mEdit);
        mFile.add(open1);
        mEdit.add(queryById);
        mEdit.add(queryByName);
        mEdit.add(addAccount);
        mEdit.add(menuItem);
        mEdit.add(menuItem1);
        menuItem.addActionListener(this);
        menuItem1.addActionListener(this);
        v[0]="姓名";v[1]="卡号";v[2]="账户种类";v[3]="密码";
        v[4]="余额";v[5]="利率";v[6]="手续";
        rowData=new Object[7][20];
        open1.addActionListener(this);
        queryById.addActionListener(this);
        queryByName.addActionListener(this);
        addAccount.addActionListener(this);
        jButton2.addActionListener(this);
        jButton3.addActionListener(this);
        int i=0;
        while (rs.next()){
            rowData[i][0]=rs.getString("name");
            rowData[i][1]=rs.getInt("id");
            rowData[i][2]=rs.getString("kind");
            if(rowData[i][2].equals("储蓄账户")){
                rowData[i][3]=rs.getString("password");
                rowData[i][4]=rs.getDouble("balance");
                x[i]=rs.getString("record");
                rowData[i][5]=rs.getDouble("others");
                rowData[i][6]=" ";
            }
            if(rowData[i][2].equals("支票账户")){
                rowData[i][3]=rs.getString("password");
                rowData[i][4]=rs.getDouble("balance");
                x[i]=rs.getString("record");
                rowData[i][6]=rs.getDouble("others");
                rowData[i][5]=" ";
            }
            if(rowData[i][2].equals("信用卡账")){
                rowData[i][3]=rs.getString("password");
                rowData[i][4]=rs.getDouble("balance");
                s=rs.getString("record");
                rowData[i][5]=" ";
                rowData[i][6]=" ";
            }
            i++;
        }
        table = new JTable(rowData,v);
        JScrollPane jScrollPane=  new JScrollPane(table);
        jf.add(jScrollPane);
        jf.setSize(500,300);
        jf.setLocation(300,200);
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }

    @Override
    public void actionPerformed(ActionEvent e){
        if (e.getSource()==open1){
            JFrame jFrame=new JFrame("查询历史记录");
            JPanel jPanel=new JPanel(new FlowLayout());
            jPanel.add(jTextField1);jPanel.add(jButton1);
            jFrame.add(jPanel);
            jFrame.setSize(400,100);
            jFrame.setLocation(400,300);
            jFrame.setVisible(true);
        }
        if (jButton1==e.getSource()){
            for (int i = 0; i < 20; i++) {
                    if(Integer.parseInt(table.getValueAt(i,1).toString())==Integer.parseInt(jTextField1.getText())){
                        System.out.println(x[i]);
                        JOptionPane.showMessageDialog(null, x[i]);
                    }
            }
        }
        if (e.getSource()==queryById){
            JFrame jFrame1=new JFrame("用ID查询");
            JPanel jPanel1=new JPanel(new FlowLayout());
            jPanel1.add(jTextField2);jPanel1.add(jButton2);
            jFrame1.add(jPanel1);
            jFrame1.setSize(400,100);
            jFrame1.setLocation(400,300);
            jFrame1.setVisible(true);
        }
        if (jButton2==e.getSource()){
            for (int i = 0; i < 20; i++) {
                if(Integer.parseInt(table.getValueAt(i,1).toString())==Integer.parseInt(jTextField2.getText())){
                    //System.out.println(x[i]);
                    String m=new String("");
                    int k=0;
                    for (int j = 0; j < 7; j++) {
                        m+=v[k]+":";
                        m+=table.getValueAt(i,j).toString();
                        m+=" ";
                        k++;
                    }
                    JOptionPane.showMessageDialog(null,m );
                }
            }
        }
        if (e.getSource()==queryByName){
            JFrame jFrame2=new JFrame("用姓名查");
            JPanel jPanel2=new JPanel(new FlowLayout());
            jPanel2.add(jTextField3);jPanel2.add(jButton3);
            jFrame2.add(jPanel2);
            jFrame2.setSize(400,100);
            jFrame2.setLocation(400,300);
            jFrame2.setVisible(true);
        }
        if (jButton3==e.getSource()){
            for (int i = 0; i < 20; i++) {
                if(table.getValueAt(i,0).toString().equals(jTextField3.getText())){
                    //System.out.println(x[i]);
                    String m=new String("");
                    int k=0;
                    for (int j = 0; j < 7; j++) {
                        m+=v[k]+":";
                        m+=table.getValueAt(i,j).toString();
                        m+=" ";
                        k++;
                    }
                    JOptionPane.showMessageDialog(null,m );
                }
            }
        }
        if (e.getSource()==addAccount){
            JOptionPane.showMessageDialog(null, "请到主界面使用注");
        }
        if(e.getSource()==menuItem){
            JFrame jFrame=new JFrame("修改信息");
            JPanel jPanel=new JPanel(new GridLayout(3,3,5,10));
            jPanel.add(jLabel101);j101=new JComboBox(item);jPanel.add(j101);jPanel.add(jTextField101);
            jPanel.add(jLabel102);j102=new JComboBox(items);jPanel.add(j102);jPanel.add(jTextField102);
            jPanel.add(jLabel103);jPanel.add(jButton11);jPanel.add(jLabel104);
            jFrame.add(jPanel);
            jFrame.setSize(300,200);
            jFrame.setLocation(400,300);
            jFrame.setVisible(true);
        }
        if (e.getSource()==jButton11){
            String s1,s2;
            if(j101.getSelectedItem().equals("name")){
                s2="'"+jTextField101.getText()+"'";
            }
            else{
                s2=jTextField101.getText();
            }
            if(j101.getSelectedItem().equals("name")){
                s1="'"+jTextField102.getText()+"'";
            }
            else{
                s1=jTextField102.getText();
            }
            String sql="update accounts set "+j101.getSelectedItem()+"="+s1+" where "+j102.getSelectedItem()+"="+s2;
            System.out.println(sql);
            try {
                Statement statement=conn.createStatement();
                int a=statement.executeUpdate(sql);
                if(a!=0) JOptionPane.showMessageDialog(null, "更改成功");
                else{
                    JOptionPane.showMessageDialog(null, "数据非法！！", "警告",JOptionPane.WARNING_MESSAGE);
                }
            } catch (SQLException throwables) {
                JOptionPane.showMessageDialog(null, "数据非法！！", "警告",JOptionPane.WARNING_MESSAGE);
            }



        }
        if (e.getSource()== menuItem1){
            JFrame jFrame=new JFrame("删除账户");
            JPanel jPanel=new JPanel(new FlowLayout());
            jComboBox = new JComboBox(item);
            jPanel.add(jComboBox);
            jPanel.add(jTextField12);jPanel.add(jButton12);
            jFrame.add(jPanel);
            jFrame.setSize(400,100);
            jFrame.setLocation(400,300);
            jFrame.setVisible(true);
        }
        if(e.getSource()==jButton12){
            String s1=jTextField12.getText();
            String sql;
            if(s1.equals("id")){
                sql="delete from accounts where "+jComboBox.getSelectedItem()+"="+s1;
            }
            else{
                sql="delete from accounts where "+jComboBox.getSelectedItem()+"='"+s1+"'";
            }
            try {
                Statement statement=conn.createStatement();
                int a=statement.executeUpdate(sql);
                if(a!=0) JOptionPane.showMessageDialog(null, "删除成功");
                else{
                    JOptionPane.showMessageDialog(null, "查无此人", "警告",JOptionPane.WARNING_MESSAGE);
                }
            } catch (SQLException throwables) {
                JOptionPane.showMessageDialog(null, "数据非法！！", "警告",JOptionPane.WARNING_MESSAGE);
            }

        }
    }
}
